class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/kyleconroy/sqlc/archive/v1.18.0.tar.gz"
  sha256 "26c932632b0ec6273778e47b6c3de54d462836be30ae0ba24cf80e267da666ac"
  license "MIT"
  head "https://github.com/kyleconroy/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_ventura:  "821278be1d5180f7e4e4db43d2bbe023d83b981a80876bc1126922f14650bf22"
    sha256 cellar: :any_skip_relocation, arm64_monterey: "e241d9e6ebba2c37af3e25bd7852d62d8d92b0656ec03845c81b9ba226a063b2"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "205febc540e6b90be284de38deb02941fcc47aac090f1077862ddfa9eb55fdae"
    sha256 cellar: :any_skip_relocation, ventura:        "e28794522861bc861d561ec9d658f191d83e2366d01bac33628c5b5428132fb5"
    sha256 cellar: :any_skip_relocation, monterey:       "30bd5dcd51a89f3ed55473eae46e7ed4a336cc90e6906e3a9ead86ac36ad0ce0"
    sha256 cellar: :any_skip_relocation, big_sur:        "8a125833d2286788ab342bcdbcc527fc44b2ab3cea8778caacc761ef5ccda8ca"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "d0aaa87c4e1bbe3af434cac56c062c042f507fd6bcbf1929f96fad7e01206606"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~SQLC
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    SQLC

    (testpath/"query.sql").write <<~EOS
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    EOS

    system bin/"sqlc", "generate"
    assert_predicate testpath/"db.go", :exist?
    assert_predicate testpath/"models.go", :exist?
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
